﻿using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Windows.Forms;
using MediaPortal.GUI.Library;
using MediaPortal.Profile;
using MPMovieCatalog;
using MPMovieCatalog.Plugin.Forms;
using MediaPortal.Configuration;
using log4net.Appender;
using log4net.Config;
using log4net.Core;
using log4net.Layout;

namespace MPMovieCatalog.Plugin
{
    public class MPMovieCatalogPlugin : IPlugin, ISetupForm
    {
        #region IPlugin Members

        public void Start()
        {
            int port;
            string logLevel;
            using (Settings settings = new Settings(Config.GetFile(Config.Dir.Config, "MPMovieCatalog.xml")))
            {
                port = settings.GetValueAsInt("default", "port", 8080);
                logLevel = settings.GetValueAsString("default", "logLevel", "INFO");
            }

            ConfigureLog(logLevel);

            MPMovieCatalogServer.Start(port);
        }

        public void Stop()
        {
        }

        #endregion

        #region ISetupForm Members

        public string Author()
        {
            return "adamanth";
        }

        public bool CanEnable()
        {
            return true;
        }

        public bool DefaultEnabled()
        {
            return true;
        }

        public string Description()
        {
            return "Server for MPMovieCatalog";
        }

        public bool GetHome(out string strButtonText, out string strButtonImage, out string strButtonImageFocus, out string strPictureImage)
        {
            strButtonText = String.Empty;
            strButtonImage = String.Empty;
            strButtonImageFocus = String.Empty;
            strPictureImage = String.Empty;
            return false;
        }

        public int GetWindowId()
        {
            return 765123;
        }

        public bool HasSetup()
        {
            return true;
        }

        public string PluginName()
        {
            return "MPMovieCatalogServer";
        }

        public void ShowPlugin()
        {
            using (SetupForm setupForm = new SetupForm())
            {
                using (Settings settings = new Settings(Config.GetFile(Config.Dir.Config, "MPMovieCatalog.xml")))
                {
                    setupForm.Port = settings.GetValueAsString("default", "port", "8080");
                    if (setupForm.ShowDialog() == DialogResult.OK)
                    {
                        settings.SetValue("default", "port", setupForm.Port);
                    }
                }
            }
        }

        #endregion

        private void ConfigureLog(string logLevel)
        {
            FileAppender appender = new FileAppender();
            appender.Layout = new PatternLayout("%date %-5level %logger - %message%newline");
            appender.File = Config.GetFile(Config.Dir.Database, @"MPMovieCatalog.log");
            appender.AppendToFile = false;

            LevelMap levelMap = new LevelMap();
            appender.Threshold = levelMap[logLevel];

            BasicConfigurator.Configure(appender);
        }
    }
}